package com.marveal.rabbit.producer.broker;

import com.marveal.rabbit.api.Message;
import com.marveal.rabbit.api.MessageProducer;
import com.marveal.rabbit.api.MessageType;
import com.marveal.rabbit.api.SendCallback;
import com.marveal.rabbit.api.exception.MessageRuntimeException;
import com.marveal.rabbit.api.exception.MessageRuntimeExceptionType;
import com.marveal.rabbit.common.util.Assert;

import java.util.List;

/**
 * $ProducerClient 发送消息的实际实现类
 */
public class ProducerClient implements MessageProducer {

    private final RabbitBroker rabbitBroker;

    public ProducerClient(RabbitBroker rabbitBroker) {
        this.rabbitBroker = rabbitBroker;
    }

    @Override
    public void send(Message message) throws MessageRuntimeException {
        Assert.notEmpty(message.getTopic(), MessageRuntimeExceptionType.MESSAGE_TOPIC_IS_EMPTY.description);

        MessageType messageType = message.getMessageType();
        switch (messageType) {
            case RAPID:
                rabbitBroker.rapidSend(message);
                break;
            case CONFIRM:
                rabbitBroker.confirmSend(message);
                break;
            case RELIANT:
                rabbitBroker.reliantSend(message);
                break;
            default:
                throw new MessageRuntimeException(MessageRuntimeExceptionType.MESSAGE_TYPE_IS_NOT_EXIST);
        }

    }

    @Override
    public void send(List<Message> messageList) throws MessageRuntimeException {
        messageList.forEach((message) -> {
            message.setMessageType(MessageType.RAPID);
            MessageHolder.add(message);
        });
        rabbitBroker.sendMessages();
    }

    @Override
    public void send(Message message, SendCallback callback) throws MessageRuntimeException {

    }
}
